﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLTV_V2.DataAccess;
using QLTV_V2.BussinessLogic;

namespace QLTV_V2
{
    public partial class FrmGiahanThe : DevComponents.DotNetBar.Office2007RibbonForm
    {
        ConnectData conn = new ConnectData();
        //NhomBanDocBUS nbdBUS = new NhomBanDocBUS();
        Utilities uti = new Utilities();
        BanDocBUS bdBUS = new BanDocBUS();
        public string mabandoc;
        public FrmGiahanThe()
        {
            InitializeComponent();
        }

        private void FrmGiahanThe_Load(object sender, EventArgs e)
        {
            //Load comboBOX nhom ban doc
            DataTable tb = uti.getDataSource("NhomBanDoc", "TenNhom");
            comb_NhomBanDoc.DataSource = tb;
            DataRow dr = tb.NewRow();
            dr["MaNhom"] = "all";
            dr["TenNhom"] = "Tất cả";
            tb.Rows.InsertAt(dr, 0);
            comb_NhomBanDoc.DisplayMember = "TenNhom";
            comb_NhomBanDoc.ValueMember = "MaNhom";
            comb_NhomBanDoc.SelectedIndex = 0;
            //Load ngay hien tai cho DateTime_Ngaylap
            timer1.Enabled = true;
            DateTime now = System.DateTime.Now;
            date_Giahanden.Value = now;
            date_Giahanden.Value = now;
            //
            dgv_BanDoc.DataSource = conn.GetDataTable(getSql());
        }
        private string getSql()
        {
            string sql = "select a.MaBanDoc, TenBanDoc, TenNhom, NgayLapThe, NgayHetHan" +
                        " from BanDoc a join NhomBanDoc b on a.MaNhom=b.MaNhom";
            if (comb_NhomBanDoc.SelectedIndex != 0)
                sql += " and a.MaNhom='" + comb_NhomBanDoc.SelectedValue.ToString()+ "'";
            if (txt_MaBandoc.Text != "")
                sql += " and MaBanDoc like '%" + txt_MaBandoc.Text + "%'";
            return sql;
        }
        private void btn_Luu_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn gia hạn không?", "Thông báo",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                int c = dgv_BanDoc.Rows.Count - 1;
                dgv_BanDoc.CurrentCell = dgv_BanDoc.Rows[c].Cells[0];
                dgv_BanDoc.CurrentCell = dgv_BanDoc.Rows[0].Cells[0];
                int kt = 0;
                int dem = 0;
                DateTime ngay = date_Giahanden.Value;
                int count = dgv_BanDoc.Rows.Count - 1;
                for (int i = 0; i < count; i++)
                {
                    if (Convert.ToBoolean(dgv_BanDoc.Rows[i].Cells["check"].Value) == true)
                    {
                        dem++;
                        string mabandoc = dgv_BanDoc.Rows[i].Cells["MaBanDoc"].Value.ToString();
                        if (bdBUS.GiaHanBD(mabandoc, ngay) ==true)
                            kt += 1;
                    }
                }
                if (dem == 0) //Nếu chưa chọn
                    MessageBox.Show("Chưa chọn thẻ bạn đọc");
                else
                {
                    if (MessageBox.Show("Gia hạn thành công " + kt + " thẻ bạn đọc! Tiếp tục ?", "Thông báo",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                    {
                        this.Close();
                    }
                    else
                    {
                        btn_cancleSelect_Click(sender, e);
                        FrmGiahanThe_Load(sender, e);
                    }
                }
            }
        }

        private void comb_NhomBanDoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            dgv_BanDoc.DataSource = conn.GetDataTable(getSql());
        }

        private void txt_MaBandoc_TextChanged(object sender, EventArgs e)
        {
            dgv_BanDoc.DataSource = conn.GetDataTable(getSql());
        }

        private void btn_selectAll_Click(object sender, EventArgs e)
        {
            int count = dgv_BanDoc.Rows.Count - 1;
            for (int i = 0; i <count; i++)
                dgv_BanDoc.Rows[i].Cells["check"].Value = true;
        }

        private void btn_cancleSelect_Click(object sender, EventArgs e)
        {
            int count = dgv_BanDoc.Rows.Count - 1;
            for (int i = 0; i < count; i++)
                dgv_BanDoc.Rows[i].Cells["check"].Value = false;
        }

        private void btn_Huy_Click(object sender, EventArgs e)
        {
            txt_MaBandoc.Text = "";
            comb_NhomBanDoc.SelectedIndex = 0;
            FrmGiahanThe_Load(sender, e);
        }
    }
}
